<?php
namespace Home\Controller;
use Think\Controller;
class BaseController extends Controller {
    Public $adminInfo;
    public $channelId;

    Public function __construct(){
        parent::__construct();
        $this->checkLogin();
        $this->channelId = $this->getChannelByOrg(); // 频道ID
    }

    public function checkLogin(){
        $id=session('organ_id');
        $is_admin = session('is_admin');

        if(session('organ_login') && $id){
            // 管理员
            if($is_admin) {
                $res=M('Admin')->find($id);
                $res2=M('Org')->where(array('admin_id'=>$id))->find();
                if($res){
                    $res['realname']=$res2['name'];
                    $this->adminInfo=$res;
                }else{
                    $this->error('请登录',U('Index/login'));
                }
            } else {
                // 记者
                $res=M('UserInfo')->find($id);
                $user_organ = M('Channel')->where(array('chid' => $res['userchannel']))->getField('org_id');
//                $power = M('UserPower')->where(array('user_id' => $res['uid']))->find();

//                $res2=M('Org')->where(array('id'=>$res['userorg']))->find();
                if($res){
//                    $res['realname']=$res2['name'];
                    $this->adminInfo=$res;
//                    $this->adminInfo['organ'] = $power['org_id'];
                    $this->adminInfo['organ'] = $user_organ;
                }else{
                    $this->error('请登录',U('Index/login'));
                }
            }


        }else{

            header("Location:".U('Index/login'));
            exit;
        }
        $this->assign('adminInfo',$res);
    }


    public function building(){
        C('TOKEN_ON',false);
        $this->display();
    }

    /**
     * 根据指定的机构获取到机构下的频道
     */
    public function getChannelByOrg() {
        $is_admin = session('is_admin');

        if($is_admin) {
            $organ_id = $this->adminInfo['organ'];
            $c_ids = array();
            if($organ_id) {
                $c_arr = M('Channel')->where(array('org_id' => $organ_id))->select();

                if($c_arr) {
                    foreach($c_arr as $key => $val) {
                        $c_ids[$key] = $val['chid'];
                    }
                }
            }
        } else {
//            $organ_id = $this->adminInfo['userorg'];
            $c_ids = array();

            $power = M('UserPower')->where(array('user_id' => $this->adminInfo['uid'],'org_id' => $this->adminInfo['organ']))->select();
            if($power) {
                foreach($power as $key => $val) {
                    $c_ids[$key] = $val['channel_id'];
                }
            }
            

            /*if($organ_id) {
                $c_arr = M('Channel')->where(array('org_id' => $organ_id))->select();

                if($c_arr) {
                    foreach($c_arr as $key => $val) {
                        $relation = unserialize($val['relation']);
                        if(in_array($this->adminInfo['uid'], $relation)) {
                            $c_ids[$key] = $val['chid'];
                        }

                    }
                }

                $c_ids[] = $this->adminInfo['userchannel']; // 自己的频道
            }*/
        }


        return $c_ids;
    }
}